$(function(){
    show_cart_list();
    show_total_count();
    show_amount()
    add_button_event()

})
function show_page_element(selector,content){
    $(selector).html(content)
}
function get_local_Storage(key){
    return localStorage.getItem(key)
}
function save_local_Storage(key,value){
    localStorage.setItem(key,value)
}

function show_total_count() {
    var total_count=get_local_Storage("item_total_count")||0;
    show_page_element("#goods_count",total_count)
}
function get_cart_list(){
    return JSON.parse(localStorage.getItem("items_in_cart"))||[];
}
function update_total_count(change_count){
    var new_total_count=parseFloat(get_local_Storage("item_total_count")||0)+change_count;
    save_local_Storage("item_total_count",new_total_count)
}
function show_cart_list(){
    var cart_list=get_cart_list()
    $('#model').tmpl(cart_list).appendTo('#table_head');
}

function add_button_event(){
    $.getJSON("../javascript/promotion_items.json",function(promotion_list){
        $("td :button").click(function (){
            var id= this.id;
            var item_id=get_item_id(id);
            var change_count= get_change_count(id)
            var cart_list=get_cart_list();
            update_current_item(promotion_list,item_id,change_count,cart_list)
            update_total_count(change_count);
            show_new_cart_list_page(item_id,cart_list)
            is_cart_empty()
        })
    })
}

function show_new_cart_list_page(item_id,cart_list){
    show_current_item(item_id,cart_list)
    show_amount()
    show_total_count()
}
function get_item_id(id){
    return id.substr(0,id.length-3);
}
function get_change_count(id){
    return id.match('sub') ? -1 : 1
}
function remove_page_item(item_id){
        $("#"+item_id+"number").parents('tr').remove();
}
function remove_local_item(cart_list){
    _.remove(cart_list,function(item){
        return item.count==0;
    })
    return cart_list
}
function is_cart_empty(){
    var cart_list=get_cart_list();
    if(cart_list.length==0){
        window.location.href='../html/item_list.html';
    }
}
function show_current_item_count(item_id,current_item){
    $("#"+item_id+"number").val(current_item.count)
}
function show_current_item(item_id,cart_list){
    var current_item=find_current_item(item_id,cart_list)
    if(current_item==undefined) {
        remove_page_item(item_id)
    }else{
        show_current_item_count(item_id, current_item)
        show_subtotal(item_id, current_item)
    }
}
function update_current_item(promotion_list,item_id,change_count,cart_list){
    var current_item=find_current_item(item_id,cart_list)
        current_item.count += change_count;
        current_item.free_count = get_free_count(promotion_list,item_id,current_item.count);
    var new_cart_list=remove_local_item(cart_list)
        save_local_Storage("items_in_cart",JSON.stringify(new_cart_list));
}

function show_subtotal(item_id,current_item){
    var subtotal = (current_item.count)*current_item.Price+"元"
    if ( current_item.free_count> 0) {
        subtotal = (current_item.count-current_item.free_count)* current_item.Price+"元(" + "原价:" + "&nbsp;&nbsp;&nbsp;" + (current_item.count)*current_item.Price + "元)"
    }
    show_page_element("#" + item_id + "subtotal",subtotal)
}
function find_current_item(item_id,cart_list){
    return _.find(cart_list,function(item){
        return item.id==item_id;
    })
}
function get_free_count(promotion_list,item_id,current_item_count){
        var free_count=0;
        if((promotion_list[0].id).indexOf(item_id) >= 0){
            free_count=Math.floor(current_item_count/3)
        }
    return free_count
}

function update_amount(){
    var items_in_cart=get_cart_list();
    var amount=settle_amount(items_in_cart)
    return amount.toFixed(2);
}
function settle_amount(items_in_cart){
    var amount=0;
    _.each(items_in_cart,function(item){
        amount += item.Price*item.count;
    })
    return amount;
}
function show_amount(){
    var amount=update_amount()
    show_page_element("#amount",amount)
}
